#include <iostream>
#include <cstring>

using std::cout;
using std::endl;

class Computer
{
public:
    static Computer* getInstance(){
        if(_pinstance == nullptr){
            _pinstance = new Computer();
        }
        return _pinstance;
    }

    static void destroy(){
        if(_pinstance != nullptr){
            delete _pinstance;
            _pinstance = nullptr;
        }
    }
    void print() const{
        cout << "name: " << _name;
        cout << " price: " << _price;
        cout << endl;
    }
    void init(const char* name, double price){
        delete [] _name;
        _name = new char[strlen(name) + 1] ();
        _price = price;
        strcpy(_name, name);
    }

private:
    Computer(const char* name = nullptr, double price = 0)
        : _name(new char[1] ())
        , _price(price){
        cout << "Computer(const char* name = nullptr, double price = 0)" << endl;
    }

    ~Computer(){
        cout <<  "~Computer()" << endl;
        if(_name != nullptr){
            delete [] _name;
            _name = nullptr;
        }
    }

    char* _name;
    double _price;
    static Computer* _pinstance;
};

Computer* Computer::_pinstance = nullptr;


int main(void)
{
    Computer::getInstance()->init("huzhu", 6999);
    Computer::getInstance()->print();
    Computer::getInstance()->init("zhuhu", 7999);
    Computer::getInstance()->print();

    cout << Computer::getInstance() << endl;
    cout << Computer::getInstance() << endl;

    Computer::destroy();
    Computer::destroy();

    Computer::getInstance();
    Computer::destroy();

    return 0;
}